﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class RelationshipsEditor : Form
    {
        List<Relationship> relationships;
        public RelationshipsEditor(List<Relationship> r)
        {
            relationships = r;
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            listView_relationships.BeginUpdate();
            listView_relationships.Items.Clear();
            foreach(Relationship r in relationships)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = r.active_character;
                lvi.SubItems.Add(r.passive_character);
                lvi.SubItems.Add(r.relationship);
                lvi.SubItems.Add(r.build ? "建立" : "删除");
                lvi.SubItems.Add(r.description);
                listView_relationships.Items.Add(lvi);
            }
            listView_relationships.EndUpdate();
        }

        private void RelationshipsEditor_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            relationships.Add(new Relationship() { build = true });
            UpdateDisplay();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            int idx = listView_relationships.SelectedIndices[0];
            if (idx < 0) return;
            if (MessageBox.Show(string.Format("确定要删除关系 {0} 吗？", string.Format("[{0}] {1}-{2}->{3}", relationships[idx].build ? "建立" : "删除", relationships[idx].active_character, relationships[idx].passive_character, relationships[idx].relationship)), "删除设定", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                relationships.RemoveAt(idx);
                UpdateDisplay();
            }
        }

        private void listView_relationships_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView_relationships.SelectedIndices.Count == 0) return;
            int idx = listView_relationships.SelectedIndices[0];
            if (idx < 0) return;
            RelationshipEditor re = new RelationshipEditor(relationships[idx]);
            re.ShowDialog();
            UpdateDisplay();
        }
    }
}
